﻿
Imports std
Imports xmlctrl
Imports usshare

Public Class xmlapi
    Inherits servelib

    Public Const _pathidentity As String = "/xmlapi/"

    Public Overrides Function pathidentity() As String
        Return _pathidentity
    End Function

    Protected Overrides Function homepage(ByVal context As httpContext, ByRef control As xmlcontrol) As Boolean
        Dim xmlpage As xmlpage = Nothing
        xmlpage = context.createControl(New xmlpage())

        Dim usage As xmlcontrol = Nothing
        usage = context.createControl(New xmlcontrol(Nothing, "usage"))
        xmlpage.addchild(usage)

        Dim basic As xmlcontrol = Nothing
        basic = context.createControl(New xmlcontrol(Nothing, "basic"))
        basic.text = "basic description of xmlapi"
        usage.addchild(basic)

        Dim request As xmlcontrol = Nothing
        request = context.createControl(New xmlcontrol(Nothing, "request"))
        request.text = pathidentity() + network.questionMark + "parameters"
        basic.addchild(request)

        Dim bypost As xmlcontrol = Nothing
        bypost = context.createControl(New xmlcontrol(Nothing, "post_method"))
        bypost.text = "xmlapi can also handle post method, same behavior & parameters"
        request.addchild(bypost)

        Dim parameterlist As xmlcontrol = Nothing
        parameterlist = context.createControl(New xmlcontrol(Nothing, "parameterlist"))
        request.addchild(parameterlist)

        Dim urlQuery As xmlcontrol = Nothing
        urlQuery = context.createControl(New xmlcontrol(Nothing, "url"))
        urlQuery.text = "url input, query name is " + query.url
        parameterlist.addchild(urlQuery)

        Dim grammar As xmlcontrol = Nothing
        grammar = context.createControl(New xmlcontrol(Nothing, "grammer"))
        grammar.text = query.url + "=http://fakeurl/"
        urlQuery.addchild(grammar)

        Dim keyQuery As xmlcontrol = Nothing
        keyQuery = context.createControl(New xmlcontrol(Nothing, "key"))
        keyQuery.text = "key input, query name is " + query.key
        parameterlist.addchild(keyQuery)

        grammar = context.createControl(New xmlcontrol(Nothing, "grammer"))
        grammar.text = query.key + "=fakekey"
        keyQuery.addchild(grammar)

        Dim actionQuery As xmlcontrol = Nothing
        actionQuery = context.createControl(New xmlcontrol(Nothing, "action"))
        actionQuery.text = "action input, server will response with this action, query name is " + query.action
        parameterlist.addchild(actionQuery)

        grammar = context.createControl(New xmlcontrol(Nothing, "grammar"))
        grammar.text = query.action + "={actionVerb}"
        actionQuery.addchild(grammar)

        Dim response As xmlcontrol = Nothing
        response = context.createControl(New xmlcontrol(Nothing, "response"))
        basic.addchild(response)

        Dim result As xmlcontrol = Nothing
        result = context.createControl(New xmlcontrol(Nothing, sharedResponse.result))
        result.text = "action result"
        response.addchild(result)

        Dim others As xmlcontrol = Nothing
        others = context.createControl(New xmlcontrol(Nothing, "others"))
        others.text = "other output definited in each action"
        response.addchild(others)

        Dim submit As xmlcontrol = Nothing
        submit = context.createControl(New xmlcontrol(Nothing, "submit"))
        submit.text = "submit with actionVerb " + actionVerbs(action.submit)
        usage.addchild(submit)

        Dim example As xmlcontrol = Nothing
        example = context.createControl(New xmlcontrol(Nothing, "example"))
        submit.addchild(example)

        request = context.createControl(New xmlcontrol(Nothing, "request"))
        request.text = urlgenerator.submit("http://fakeurl/", "fakekey")
        example.addchild(request)

        response = context.createControl(New xmlcontrol(Nothing, "response"))
        example.addchild(response)

        Dim success As xmlcontrol = Nothing
        success = context.createControl(New xmlcontrol(Nothing, submitResponse.success))
        success.text = "submit successfully, the mapping is available."
        response.addchild(success)

        Dim redirector As xmlcontrol = Nothing
        redirector = context.createControl(New xmlcontrol(Nothing, submitResponse.redirector))
        redirector.text = urlgenerator.jumpfull("fakekey")
        success.addchild(redirector)

        Dim invalidKey As xmlcontrol = Nothing
        invalidKey = context.createControl(New xmlcontrol(Nothing, submitResponse.invalidKey))
        invalidKey.text = "failed to submit, the key input is invalid."
        response.addchild(invalidKey)

        Dim existingKey As xmlcontrol = Nothing
        existingKey = context.createControl(New xmlcontrol(Nothing, submitResponse.existingKey))
        existingKey.text = "failed to submit, the key is already existing."
        response.addchild(existingKey)

        Dim internalError As xmlcontrol = Nothing
        internalError = context.createControl(New xmlcontrol(Nothing, submitResponse.internalError))
        internalError.text = "failed to submit, some internal errors caught."
        response.addchild(internalError)

        Dim preview As xmlcontrol = Nothing
        preview = context.createControl(New xmlcontrol(Nothing, "preview"))
        preview.text = "preview with actionVerb " + actionVerbs(action.preview) + ", get destination url by key"
        usage.addchild(preview)

        example = context.createControl(New xmlcontrol(Nothing, "example"))
        preview.addchild(example)

        request = context.createControl(New xmlcontrol(Nothing, "request"))
        request.text = urlgenerator.preview("fakekey")
        example.addchild(request)

        response = context.createControl(New xmlcontrol(Nothing, "response"))
        example.addchild(response)

        success = context.createControl(New xmlcontrol(Nothing, previewResponse.success))
        success.text = "preview successfully, the mapping has been found."
        response.addchild(success)

        Dim destination As xmlcontrol = Nothing
        destination = context.createControl(New xmlcontrol(Nothing, previewResponse.destination))
        destination.text = "http://fakeurl/"
        success.addchild(destination)

        Dim fail As xmlcontrol = Nothing
        fail = context.createControl(New xmlcontrol(Nothing, previewResponse.fail))
        fail.text = "preview failed, the mapping is not existing."
        response.addchild(fail)

        Dim urlkeys As xmlcontrol = Nothing
        urlkeys = context.createControl(New xmlcontrol(Nothing, "urlkeys"))
        urlkeys.text = "view all keys allocated to a url, with actionVerb " + actionVerbs(action.urlkeys)
        usage.addchild(urlkeys)

        example = context.createControl(New xmlcontrol(Nothing, "example"))
        urlkeys.addchild(example)

        request = context.createControl(New xmlcontrol(Nothing, "request"))
        request.text = urlgenerator.urlkeys("http://fakeurl/")
        example.addchild(request)

        response = context.createControl(New xmlcontrol(Nothing, "response"))
        example.addchild(response)

        success = context.createControl(New xmlcontrol(Nothing, urlkeysResponse.success))
        success.text = "found at least one key allocated to the url."
        response.addchild(success)

        Dim i As Int64
        i = rnd(1, 9, True)
        While i > 0
            Dim key As xmlcontrol = Nothing
            key = context.createControl(New xmlcontrol(Nothing, urlkeysResponse.key))
            key.text = urlgenerator.jumpfull("fakekey_" + generateRandomKey())
            success.addchild(key)
            i -= 1
        End While

        fail = context.createControl(New xmlcontrol(Nothing, urlkeysResponse.fail))
        fail.text = "has not found any key allocated to the url."
        response.addchild(fail)

        Dim have As xmlcontrol = Nothing
        have = context.createControl(New xmlcontrol(Nothing, "have"))
        have.text = "try whether a url has already been submited, with actionVerb " + actionVerbs(action.have)
        usage.addchild(have)

        example = context.createControl(New xmlcontrol(Nothing, "example"))
        have.addchild(example)

        request = context.createControl(New xmlcontrol(Nothing, "request"))
        request.text = urlgenerator.have("http://fakeurl/")
        example.addchild(request)

        response = context.createControl(New xmlcontrol(Nothing, "response"))
        example.addchild(response)

        success = context.createControl(New xmlcontrol(Nothing, haveResponse.success))
        success.text = "found at least one key allocated to the url."
        response.addchild(success)

        fail = context.createControl(New xmlcontrol(Nothing, haveResponse.fail))
        fail.text = "has not found any key allocated to the url."
        response.addchild(fail)

        Dim jump As xmlcontrol = Nothing
        jump = context.createControl(New xmlcontrol(Nothing, "jump"))
        jump.text = "same behavior with preview in xmlapi, with actionVerb " + actionVerbs(action.jump)
        usage.addchild(jump)

        Dim delayJump As xmlcontrol = Nothing
        delayJump = context.createControl(New xmlcontrol(Nothing, "delayJump"))
        delayJump.text = "same behavior with preview in xmlapi, with actionVerb " + actionVerbs(action.delayJump)
        usage.addchild(delayJump)

        control = xmlpage
        Return True
    End Function

    Protected Overrides Function jump(ByVal context As httpContext, ByRef control As xmlcontrol _
                                      , ByVal key2urlResult As Boolean _
                                      , ByVal key As String, ByVal url As String, ByVal delay As Int64) As Boolean
        Return preview(context, control, key2urlResult, key, url)
    End Function

    Protected Overrides Function preview(ByVal context As httpContext, ByRef control As xmlcontrol _
                                         , ByVal key2urlResult As Boolean _
                                         , ByVal key As String, ByVal url As String) As Boolean
        Dim page As xmlpage = Nothing
        page = context.createControl(New xmlpage())
        Dim result As xmlcontrol = Nothing
        result = context.createControl(New xmlcontrol(Nothing, sharedResponse.result))
        page.addchild(result)

        If key2urlResult Then
            pingVisitCount(key)
            result.text = previewResponse.success

            Dim destination As xmlcontrol = Nothing
            destination = context.createControl(New xmlcontrol(Nothing, previewResponse.destination))
            destination.text = url
            result.addchild(destination)
        Else
            result.text = previewResponse.fail
        End If

        control = page
        Return True
    End Function

    Protected Overrides Function submit(ByVal context As httpContext, ByRef control As xmlcontrol, _
                                        ByVal submitResult As submitResult, _
                                        ByVal key As String, ByVal url As String) As Boolean
        Dim page As xmlpage = Nothing
        page = context.createControl(New xmlpage())

        Dim result As xmlcontrol = Nothing
        result = context.createControl(New xmlcontrol(Nothing, sharedResponse.result))
        page.addchild(result)

        Select Case submitResult
            Case submitResult.success
                result.text = submitResponse.success

                Dim redirector As xmlcontrol = Nothing
                redirector = context.createControl(New xmlcontrol(Nothing, submitResponse.redirector))
                redirector.text = urlgenerator.jumpfull(key)
                result.addchild(redirector)
            Case submitResult.existingKey
                result.text = submitResponse.existingKey
            Case submitResult.internalError
                result.text = submitResponse.internalError
            Case submitResult.invalidKey
                result.text = submitResponse.invalidKey
            Case Else
                'assert here, if code runs to here, something special happened.
                assert(False, "should cover all possibilities.")
        End Select

        control = page
        Return True
    End Function

    Protected Overrides Function urlkeys(ByVal context As httpContext, ByRef control As xmlcontrol _
                                         , ByVal url2keysResult As Boolean _
                                         , ByVal url As String, ByVal keys As [set](Of String)) As Boolean
        Dim page As xmlpage = Nothing
        page = context.createControl(New xmlpage())

        Dim result As xmlcontrol = Nothing
        result = context.createControl(New xmlcontrol(Nothing, sharedResponse.result))
        page.addchild(result)

        If url2keysResult Then
            result.text = urlkeysResponse.success
            Dim it As [set](Of String).iterator = Nothing
            it = keys.begin()

            While it <> keys.end()
                Dim key As xmlcontrol = Nothing
                key = context.createControl(New xmlcontrol(Nothing, urlkeysResponse.key))
                key.text = urlgenerator.jumpfull(+it)
                result.addchild(key)

                it += 1
            End While
        Else
            result.text = urlkeysResponse.fail
        End If

        control = page
        Return True
    End Function

    Protected Overrides Function have(ByVal context As httpContext, ByRef control As xmlcontrol _
                                      , ByVal haveResult As Boolean, ByVal url As String) As Boolean
        Dim page As xmlpage = Nothing
        page = context.createControl(New xmlpage())

        Dim result As xmlcontrol = Nothing
        result = context.createControl(New xmlcontrol(Nothing, sharedResponse.result))
        page.addchild(result)

        If haveResult Then
            result.text = haveResponse.success
        Else
            result.text = haveResponse.fail
        End If

        control = page
        Return True
    End Function
End Class

Public Class xmlapi2
    Inherits xmlapi

    Public Shadows Const _pathidentity As String = "/xmlapi"

    Public Overrides Function pathidentity() As String
        Return _pathidentity
    End Function

End Class
